#include "SQLSystem.h"
#include "Command.h"


SQLSystem::SQLSystem()
{
	cmdInsert  = new InsertCommand(this);
	cmdSelect  = new SelectCommand(this);
	cmdDelete  = new DeleteCommand(this);
	cmdDrop    = new DropCommand(this);
	cmdCreate  = new CreateCommand(this);
	cmdUnload  = new UnloadCommand(this);
	cmdEbook   = new EbookCommand(this);
	cmdWebsite = new WebsiteCommand(this);
	cmdLog = new LogCommand(this);
	cmdEmail = new EmailCommand(this);
	cmdUpdate = new UpdateCommand(this);
}


SQLSystem::~SQLSystem()
{
}

vector<Table *> SQLSystem::getAllTables()
{
	return tables;
}

Table* SQLSystem::getTableByName(string name)
{
	Table* table;
	for(unsigned int i=0;i<tables.size();i++)
	{
		if(helper->toUpperString(name) == helper->toUpperString(tables[i]->getName()))
		{
			table= tables[i];
			return table;
		}
	}
	return NULL;
}

int SQLSystem::getTableIndex(string table)
{
	for(unsigned int i=0;i<tables.size();i++)
	{
		if(helper->toUpperString(table) == helper->toUpperString(tables[i]->getName()))
			return i;
	}
	return -1;
}

void SQLSystem::removeTable(string name)
{
	int index= getTableIndex(name);
	if(index == -1)
	{
		cout << "No table found\n";
		return;
	}
	tables.erase(tables.begin()+index);
}

void SQLSystem::addTable(Table* table)
{
	tables.push_back(table);
}

void SQLSystem::startup()
{
	initEbookTable();
	initWebsiteTable();
	initEmailTale();
	initLogTable();
}

void SQLSystem::input()
{
	getline(cin, statement);
}

void SQLSystem::process()
{
	

}


void SQLSystem::run()
{
	bool stop=false;
	while(!stop)
	{
		cout << "ENTER THE COMMAND: ";
		input();
		typedef tokenizer<char_separator<char> > token;
		char_separator<char> sep(" ","(),",drop_empty_tokens);
		token tok(statement, sep);
		token::iterator tok_iter= tok.begin();
		if(helper->toUpperString(*tok_iter) == "CREATE")
		{
	
			if(!cmdCreate->execute(statement))
			{
				cout << "ERROR CREATING TABLE\n";
			}
		
		}
		else if(helper->toUpperString(*tok_iter) == "INSERT")
		{
			++tok_iter;
			if(tok_iter == tok.end())	
				continue;
			if(helper->toUpperString(*tok_iter) == "EBOOK")
			{
				if(!cmdEbook->insertEbook(statement))
				{
					cout << "ERROR INSERTING RECORD\n";
				}
			}
			else if (helper->toUpperString(*tok_iter) == "WEBSITE")
			{
				if(!cmdWebsite->insertWebsite(statement))
				{
					cout << "ERROR INSERTING RECORD\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "LOG")
			{
				if(!cmdLog->insertLog(statement))
				{
					cout << "ERROR INSERTING RECORD\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "EMAIL")
			{
				if(!cmdEmail->insertEmail(statement))
				{
					cout << "ERROR INSERTING RECORD\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "INTO")
			{
				if(!cmdInsert->execute(statement))
				{
					cout << "ERROR INSERTING RECORD\n";
				}
			}
		}
		else if(helper->toUpperString(*tok_iter) == "EXIT")
			return;
		else if(helper->toUpperString(*tok_iter) == "SELECT")
		{
			
			if(!cmdSelect->execute(statement))
			{
				cout << "ERROR SELECTING RECORDS\n";
			}
		}
		else if(helper->toUpperString(*tok_iter) == "UNLOAD")
		{
			
			if(!cmdUnload->execute(statement))
			{
				cout << "ERROR UNLOADING DATA\n";
			}
		}
		else if(helper->toUpperString(*tok_iter) == "DROP")
		{
			
			if(!cmdDrop->execute(statement))
			{
				cout << "ERROR DROPPING TABLE\n";
			}
		}
		else if(helper->toUpperString(*tok_iter) == "DELETE")
		{
			++tok_iter;
			if(tok_iter == tok.end())	
				continue;
			if(helper->toUpperString(*tok_iter) == "EBOOK")
			{
			
				if(!cmdEbook->deleteEbook(statement))
				{
					cout << "ERROR DELETING RECORDS\n";
				}
			}
			if(helper->toUpperString(*tok_iter) == "WEBSITE")
			{
				if(!cmdWebsite->deleteWebsite(statement))
				{
					cout << "ERROR DELETING RECORDS\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "LOG")
			{
				
				if(!cmdLog->deleteLog(statement))
				{
					cout << "ERROR INSERTING RECORD\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "EMAIL")
			{
				
				if(!cmdEmail->deleteEmail(statement))
				{
					cout << "ERROR INSERTING RECORD\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "FROM")
			{
				if(!cmdDelete->execute(statement))
				{
					cout << "ERROR DELETING RECORDS\n";
				}
			}
		}
		else if(helper->toUpperString(*tok_iter) == "SEARCH")
		{
			++tok_iter;
			if(tok_iter == tok.end())	
				continue;
			if(helper->toUpperString(*tok_iter) == "EBOOK")
			{
				if(!cmdEbook->searchEbook(statement))
				{
					cout << "ERROR SEARCH EBOOK\n";
				}
			}else if(helper->toUpperString(*tok_iter) == "WEBSITE")
			{
				if(!cmdWebsite->searchWebsite(statement))
				{
					cout << "ERROR SEARCH WEBSITE\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "LOG")
			{
				if(!cmdLog->searchLog(statement))
				{
					cout << "ERROR SEARCHING RECORDS\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "EMAIL")
			{
				if(!cmdEmail->searchEmail(statement))
				{
					cout << "ERROR SEARCHING RECORDS\n";
				}
			}
		}
		else if(helper->toUpperString(*tok_iter) == "SORT")
		{
			++tok_iter;
			if(tok_iter == tok.end())	
				continue;
			if(helper->toUpperString(*tok_iter) == "EBOOK")
			{
				
				if(!cmdEbook->sortEbook(statement))
				{
					cout << "ERROR SORTING RECORDS\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "LOG")
			{
				
				if(!cmdLog->sortLog(statement))
				{
					cout << "ERROR SORTING RECORDS\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "EMAIL")
			{
				
				if(!cmdEmail->sortEmail(statement))
				{
					cout << "ERROR SORTING RECORDS\n";
				}
			}
			else if(helper->toUpperString(*tok_iter) == "WEBSITE")
			{
				
				if(!cmdWebsite->sort(statement))
				{
					cout << "ERROR SORTING RECORDS\n";
				}
			}
		}else if(helper->toUpperString(*tok_iter) == "SHOW")
		{
			++tok_iter;
			Table *t = getTableByName(*tok_iter);
			if( !t) {
					cout << "TABLE DOES NOT EXIST\n";
			}
			t->display();
		}else if(helper->toUpperString(*tok_iter) == "UPDATE")
		{
			if(!cmdUpdate->execute(statement))
			{
				cout << "ERROR SORTING RECORDS\n";
			}
		}
		else
			cout << "WRONG COMMAND\n";

	}
}



void SQLSystem::initWebsiteTable()
{

	Table* t;
	t= new Table();
	t->setName(constants::website());
	t->addColumn(constants::name(), VARCHAR);
	t->addColumn(constants::file_name(), VARCHAR);
	t->addColumn(constants::url(), VARCHAR);
	t->addColumn(constants::view_date(),VARCHAR);
	addTable(t);
}

void SQLSystem::initEbookTable(){
	
	Table* t;
	t= new Table();
	t->setName("EBOOK");
	t->addColumn("Name", VARCHAR);
	t->addColumn("Path", VARCHAR);
	t->addColumn("author",VARCHAR);
	t->addColumn("publisher",VARCHAR);
	t->addColumn("publish_year",VARCHAR);
	addTable(t);
}
void SQLSystem::initLogTable()
{
	Table* logTable;
	logTable= new Table();
	logTable->setName("LOG");
	logTable->addColumn(constants::name(),VARCHAR);
	logTable->addColumn(constants::file_name(),VARCHAR);
	logTable->addColumn(constants::log_date(),VARCHAR);
	addTable(logTable);
}

void SQLSystem::initEmailTale()
{
	Table* t;
	t= new Table();
	t->setName("EMAIL");
	t->addColumn(constants::subject(),VARCHAR);
	t->addColumn(constants::sender(),VARCHAR);
	t->addColumn(constants::receiver(),VARCHAR);
	t->addColumn(constants::send_date(),VARCHAR);
	addTable(t);
}
